package account.student.view;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.Toolkit;

import javax.swing.JFrame;

import prototyping.client.student.controller.HelpButtonController;
import prototyping.client.student.controller.HelpButtonControllerOverig;
import tools.FieldLimit;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.LayoutStyle.ComponentPlacement;

import java.awt.BorderLayout;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JScrollBar;
import javax.swing.JList;
import javax.swing.JTable;

import dao.server.DataLoadIP;
import account.model.Account;
import account.model.SubTeam;
import account.student.controller.StudentController;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Observable;
import java.util.Observer;

public class StudentView extends JFrame implements Observer {

	private static StudentView instance;
	private String userName;
	private String server;
	private JLabel lblUsername;
	private JTextArea txtArea;
	private HelpButtonControllerOverig hbc;

	public static StudentView getInstance(){
		StudentView instance = null;
		if (instance == null) {
			instance = new StudentView();
		}
		return instance;
	}

	private StudentView() {
		server = DataLoadIP.getInstance().LoadIP();
		userName = SubTeam.getInstance().getAcc().getPersoon().getVoornaam() + " " + SubTeam.getInstance().getAcc().getPersoon().getAchternaam();

		//Load jFrame
		initComponents();
	}

	private void initComponents() {
		//textveld
		txtArea = new JTextArea();
		txtArea.setWrapStyleWord(true);
		txtArea.setLineWrap(true);
		txtArea.setDocument(new FieldLimit(60));
		
		//bericht versturen naar docent
		hbc = new HelpButtonControllerOverig(userName, server, this.txtArea);
		
		//buttons
		JButton btnVraag = new JButton("Verzenden");
		btnVraag.addActionListener(hbc);
		
		//mogelijkheid om op eigen naam te klikken
		JLabel lblUsername = new JLabel("username");
		lblUsername.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e)   
			//opent account settings  
			{StudentAccountView.getInstance().setVisible(true);}   
		});

		lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblUsername.setText(userName);

		//menu
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnStart = new JMenu("Start");
		menuBar.add(mnStart);

		JMenuItem mntmNewMenuItem = new JMenuItem("Afmelden");
		mnStart.add(mntmNewMenuItem);
		mntmNewMenuItem.addActionListener(new StudentController(mntmNewMenuItem));

		JMenu mnProfiel = new JMenu("Profiel");
		menuBar.add(mnProfiel);

		JMenuItem mntmBewerken = new JMenuItem("Bewerken");
		mntmBewerken.addActionListener(new StudentController(mntmBewerken));
		mnProfiel.add(mntmBewerken);

		JMenu mnHelp = new JMenu("Help");
		menuBar.add(mnHelp);

		JMenuItem mntmHowTo = new JMenuItem("Voorwaarden");
		mnHelp.add(mntmHowTo);
		
		//panel
		JPanel panel = new JPanel();

		btnVraag.setActionCommand(txtArea.getText());
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
				gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_panel.createSequentialGroup()
												.addComponent(txtArea, GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
												.addContainerGap())
												.addGroup(gl_panel.createSequentialGroup()
														.addComponent(lblUsername)
														.addGap(12)))
														.addComponent(btnVraag, Alignment.TRAILING)))
				);
		gl_panel.setVerticalGroup(
				gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
						.addGap(12)
						.addComponent(lblUsername)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(txtArea, GroupLayout.PREFERRED_SIZE, 246, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(btnVraag)
						.addContainerGap(38, Short.MAX_VALUE))
				);
		panel.setLayout(gl_panel);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
				);
		groupLayout.setVerticalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(panel, GroupLayout.PREFERRED_SIZE, 331, GroupLayout.PREFERRED_SIZE)
				);
		getContentPane().setLayout(groupLayout);

		setTitle("Welkom " + userName);
		setVisible(true);
		setResizable(false);
		pack();

		//center scherm
		Dimension center = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(center.width / 2 - this.getSize().width / 2,
				center.height / 2 - this.getSize().height / 2);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		//naam updaten
		lblUsername.setText(((Account)arg1).getPersoon().getVoornaam() + " " + ((Account)arg1).getPersoon().getAchternaam());
	}
}
